package xx.iverson.file_manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xx.iverson.file_manager.entity.CategoryLevel1;
import xx.iverson.file_manager.entity.CategoryLevel2;
import xx.iverson.file_manager.entity.FileInfo;
import xx.iverson.file_manager.mapper.SysUserMapper;
import xx.iverson.file_manager.service.CategoryLevel1Service;
import xx.iverson.file_manager.service.FileInfoService;
import xx.iverson.file_manager.util.GetLocalIP;

import java.util.List;


/**
 * @author 86131
 */
@Controller
@RequestMapping
public class FileFrontController {

    @Autowired
    private FileInfoService fileService;

    @Autowired
    private CategoryLevel1Service categoryLevel1Service;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Value("${kkFileView.host}")
    private String kkFileViewServerUrl;

    /**
     * 文件列表页面，支持一级分类、二级分类筛选和搜索
     */
    @GetMapping("")
    public String fileList(
            @RequestParam(value = "level1Id", required = false) Integer level1Id,
            @RequestParam(value = "level2Id", required = false) Integer level2Id,
            @RequestParam(value = "query", required = false) String query,
            Model model) {
        model.addAttribute("title", sysUserMapper.selectTitle());

        // 获取所有一级分类及其二级分类
        List<CategoryLevel1> level1Categories = categoryLevel1Service.getAllLevel1WithLevel2();
        model.addAttribute("level1Categories", level1Categories);

        // 确定当前显示的文件列表
        List<FileInfo> currentFiles;
        String currentCategoryName = "全部资源";

        if (level2Id != null && level2Id > 0) {
            // 按二级分类筛选
            if (query != null && !query.trim().isEmpty()) {
                // 二级分类+搜索
                currentFiles = fileService.searchFilesByCategoryAndName(level2Id, query);
            } else {
                // 仅二级分类
                currentFiles = fileService.getFilesByLevel2Id(level2Id);
            }

            // 获取当前分类名称
            for (CategoryLevel1 level1 : level1Categories) {
                for (CategoryLevel2 level2 : level1.getLevel2Categories()) {
                    if (level2.getId().equals(level2Id)) {
                        currentCategoryName = level1.getName() + " - " + level2.getName();
                        break;
                    }
                }
            }

        } else if (level1Id != null && level1Id > 0) {
            // 按一级分类筛选
            if (query != null && !query.trim().isEmpty()) {
                // 一级分类+搜索
                currentFiles = fileService.searchFilesByLevel1AndName(level1Id, query);
            } else {
                // 仅一级分类
                currentFiles = fileService.getFilesByLevel1Id(level1Id);
            }

            // 获取当前分类名称
            for (CategoryLevel1 level1 : level1Categories) {
                if (level1.getId().equals(level1Id)) {
                    currentCategoryName = level1.getName();
                    break;
                }
            }

        } else if (query != null && !query.trim().isEmpty()) {
            // 全局搜索
            currentFiles = fileService.searchFilesByName(query);
            currentCategoryName = "搜索结果: \"" + query + "\"";
        } else {
            // 显示所有文件
            currentFiles = fileService.getAllFiles();
        }

        // 向模型添加属性
        model.addAttribute("currentFiles", currentFiles);
        model.addAttribute("currentLevel1Id", level1Id);
        model.addAttribute("currentLevel2Id", level2Id);
        model.addAttribute("currentCategoryName", currentCategoryName);
        model.addAttribute("currentQuery", query);
        model.addAttribute("kkFileViewServerUrl", kkFileViewServerUrl);

        return "index";
    }

    @GetMapping("getIp")
    @ResponseBody
    public String getIp() throws Exception {
        return GetLocalIP.getLocalIp();
    }
}